package com.example.shop_2208.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.example.shop_2208.R
import com.example.shop_2208.adapter.CategoryAdapter
import com.example.shop_2208.adapter.GoodsAdapter
import com.example.shop_2208.adapter.OneAdapter
import com.example.shop_2208.databinding.FragmentCateBinding
import com.example.shop_2208.mvvm.BaseFragment
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class CateFragment : BaseFragment<FragmentCateBinding,CateViewModel>() {
    lateinit var oneAdapter: OneAdapter//一级分类
    lateinit var categoryAdapter: CategoryAdapter//二级分类
    lateinit var goodsAdapter: GoodsAdapter//三级分类商品
    var cateId = 0
    var page = 1
    override fun initData() {

        viewModel.cateSuccess.observe(this){
            if(it.code==200){
                //加载一级分类
                oneAdapter.data.clear()
                oneAdapter.data.addAll(it.data)
                oneAdapter.notifyDataSetChanged()
                //加载二级分类
                val data = oneAdapter.data[0].data
                if(data!=null){
                    categoryAdapter.data.clear()
                    categoryAdapter.data.addAll(data)
                    categoryAdapter.notifyDataSetChanged()
                    //加载三级分类
                    cateId = data[0].id
                    viewModel.goods(cateId,page,10)

                }
            }
        }
        viewModel.goodsSuccess.observe(this){
            if(it.code==200){
                if(page==1){
                    goodsAdapter.data.clear()
                }
                goodsAdapter.data.addAll(it.data)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView() {
        //初始化所有的rv
        oneAdapter = OneAdapter()
        binding.rvOne.adapter = oneAdapter
        binding.rvOne.layoutManager = LinearLayoutManager(context)

        categoryAdapter = CategoryAdapter()
        binding.rvTwo.adapter = categoryAdapter
        binding.rvTwo.layoutManager = LinearLayoutManager(context)

        goodsAdapter = GoodsAdapter()
        binding.rvGoods.adapter = goodsAdapter
        binding.rvGoods.layoutManager = LinearLayoutManager(context)

        viewModel.category()

        //一级分类点击事件
        oneAdapter.setOnItemClickListener { adapter, view, position ->
            //实现变色
            oneAdapter.setSelectedId(oneAdapter.data[position].id)
            //二级分类和三级分类清空
            categoryAdapter.data.clear()
            goodsAdapter.data.clear()

            //获取二级分类的data
            val data = oneAdapter.data[position].data
            if(data!=null){
                if(data.isNotEmpty()){
                    categoryAdapter.data.addAll(data)
                    categoryAdapter.setId(data[0].id)//点击一级分类时，二级分类的第一个item添加背景色
                    //获取三级分类
                    cateId = data[0].id
                    page=1
                    viewModel.goods(cateId,page,10)
                }
            }
            categoryAdapter.notifyDataSetChanged()
            goodsAdapter.notifyDataSetChanged()
        }
        //二级分类点击事件
        categoryAdapter.setOnItemClickListener { adapter, view, position ->
            categoryAdapter.setId(categoryAdapter.data[position].id)

            cateId = categoryAdapter.data[position].id
            page = 1
            viewModel.goods(cateId,page,10)
        }
        //三级分类的点击事件
        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/detail").withInt("goodsId",goodsAdapter.data[position].id).navigation()
        }
        binding.srl.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.goods(cateId,page,10)
                binding.srl.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.goods(cateId,page,10)
                binding.srl.finishLoadMore()
            }
        })

    }

    override fun getViewModel(): Class<CateViewModel>  = CateViewModel::class.java

    override fun getLayout(): Int  = R.layout.fragment_cate

}